package com.molyfun.parents.activity.store.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.coursecenter.ProductDetailActivity;
import com.molyfun.parents.rest.bean.UserProductListResponse;
import com.molyfun.parents.utils.StringUtils;
import com.molyfun.parents.utils.TimeUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class ProductBriefView extends LinearLayout implements View.OnClickListener{

    protected LayoutInflater layoutInflater;

    @InjectView(R.id.product_brief_title)
    LinearLayout mProductBriefTitle;
    @InjectView(R.id.product_name)
    TextView mProductName;
    @InjectView(R.id.student_remarkname)
    TextView mStudentRemarkName;
    @InjectView(R.id.organization_name)
    TextView mOrganizationName;
    @InjectView(R.id.time_interval)
    TextView mTimeInterval;
    @InjectView(R.id.product_list_layout)
    LinearLayout mProductListLayout;

    private Context context;
    private UserProductListResponse.ProductBean mProductBean;

    public ProductBriefView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ProductBriefView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ProductBriefView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_product_brief, this);
        ButterKnife.inject(this);
    }

    public void initData(UserProductListResponse.ProductBean productBean) {
        if(productBean == null){
            return;
        }
        mProductBean = productBean;
        mProductName.setText(productBean.getName());
        mTimeInterval.setText(TimeUtils.getDateByTimeMillis(productBean.getEnddatetime()));
        mProductBriefTitle.setOnClickListener(this);
        mOrganizationName.setText(productBean.getDepartname());
        mProductListLayout.removeAllViews();
        mStudentRemarkName.setText("(" + productBean.getStudentremarkname() + ")");
        if(productBean.getProductitemlist() != null && productBean.getProductitemlist().size() > 0){
            for(UserProductListResponse.ProductBean.ProductitemlistBean productitemlistBean : productBean.getProductitemlist()){
                LinearLayout layout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.layout_product_brief_item, null, false);
                ((TextView)layout.findViewById(R.id.product_item_title)).setText(StringUtils.getSpltString(productitemlistBean.getName(), 10));
                ((TextView)layout.findViewById(R.id.product_item_count)).setText(productitemlistBean.getTimes() + "次");
                layout.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //TODO 跳转到详情页
                    }
                });
                mProductListLayout.addView(layout);
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.product_brief_title:
                ProductDetailActivity.startActivity(context, mProductBean.getProductid(),mProductBean.getDepartname());
                break;
            default:
                break;
        }
    }


}